/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true

    },
    {
        id: 'realName',
        title: '姓名',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'mobilePhone',
        title: '手机号码',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'firLevelFriendsNum',
        title: '一级好友(人)',
        type: 'int',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false
    },
    {
        id: 'secLevelFriendsNum',
        title: '二级好友(人)',
        type: 'int',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false
    },
    {
        id: 'operation',
        title: '操作',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return '<a href="javascript:;" name="showChildren" data-dataNo="' + dataNo + '" class="btn btn-white btn-sm"><i class="fa fa-level-down"></i>下级用户</a>';
        }
    }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + '/friends/json/list',
    exportFileName: '好友列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: ["secLevelFriendsNum", "firLevelFriendsNum"]},
    onGridComplete: function (grid) {
        var returnBtn = $("#returnParent"),
            parent = friends.current.getParent(),
            data = grid.exhibitDatas[0];
        //是否显示上一级
        if(data!=undefined){
            if (data.parentId != 0) {
                //使用code进行判断并查询数据
                returnBtn.removeClass("hidden").off("click").on("click", function () {
                    friends.current.gridGoFormCode(data.code);
                });
            } else {
                returnBtn.addClass("hidden");
            }
        }else{
            returnBtn.removeClass("hidden").off("click").on("click", function () {
                var data = friends.current.getParent();
                friends.current.gridGoFormParentId(data.parentId);
            });
        }
        $("a[name='showChildren']").on('click', function () {
            var dataNo = $(this).attr("data-dataNo");
            currentData = grid.exhibitDatas[dataNo];
            friends.current.setParent(data);
            friends.current.gridGoFormParentId(currentData.id);
        });
    }
}

var grid = $.fn.DtGrid.init(dtGridOption);
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;
grid.fastQueryParameters['eq_parentId'] = 0;


//排序
grid.sortParameter.columnId = ['asc_id'];
//列表结束

//模糊搜索
$('#customSearch').click(function () {
    grid.fastQueryParameters = new Object();
    grid.fastQueryParameters['eq_delFlag'] = 1;
    grid.fastQueryParameters['lk_real_name_or_lk_user_name_or_lk_mobile'] = $('#keyword').val();
    grid.pager.startRecord = 0;
    grid.pager.nowPage = 1;
    grid.pager.recordCount = -1;
    grid.pager.pageCount = -1;
    grid.refresh(true);
});

/**
 * 自定义相关
 */
var friends = function ($) {
    var _current = {
        /**
         * 储存用户列表
         */
        $$parent: '',
        /**
         * 修改选择的用户列表
         * @param obj
         */
        setParent: function (obj) {
            obj && (this.$$parent = obj)
        },
        /**
         * 返回列表中的用户
         * @returns {*}
         */
        getParent: function () {
             return this.$$parent;
        },
        /**
         * 删除
         */
        delParent: function () {
            delete this.$$parent;
            this.$$parent='';
        },
        gridGoFormParentId: function (parentId) {
            grid.option.loadURL=ncGlobal.adminRoot + '/friends/json/list';
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['eq_parentId'] = parentId;
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        },
        gridGoFormCode: function (code) {
            grid.fastQueryParameters = new Object();
            if(code.length>=5){
                var val=code.substring(0,code.length-4);
                grid.option.loadURL=ncGlobal.adminRoot+"/partner/json/list/"+val+ "/" + new Date().getTime();
            }else {
                grid.option.loadURL=ncGlobal.adminRoot + '/friends/json/list';
                grid.fastQueryParameters['eq_parentId'] = 0;
            }
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        }
    };
    return {
        init: function () {
        },
        current: _current
    };
}(jQuery);
//操作处理结束

$(function () {
    //加载列表
    grid.load();
});